import { createStoreBindings } from "mobx-miniprogram-bindings"
import { store } from "../../store/store"
import toast from "../../miniprogram_npm/@vant/weapp/toast/toast"

var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        rechargeMoney: null,
        money: 0,
        nowMoney: 0,
        now: '',
        userInfo: {},
        orderDetail: [],
    },

    initUserInfo() {
        if (!wx.getStorageSync('userInfo').userid) {
            toast('请先登陆')
        } else {
            this.getUserInfo(wx.getStorageSync('userInfo').userid)
        }
    },

    stop() {
        if (!wx.getStorageSync('userInfo').userid) {
            toast('请先登陆')
            return
        } 
        var userid = wx.getStorageSync('userInfo').userid
        wx.p.request({
            method: 'POST',
            url: app.globalData.api + 'order/stop',
            data: {
                userid: userid
            }
        }).then(res => {
            if (res.data.success) {
                toast('操作成功')
                this.initOrderDetail()
            } else {
                toast(res.data.msg)
            }
        })
    },

    async getUserInfo(userid) {
        await wx.p.request({
            method: 'POST',
            url: app.globalData.api + 'user/getUserById',
            data: {
            userid: userid
            }
        }).then(res => {
            this.setData({
                userInfo: res.data,
                money: (res.data.money / 100).toFixed(2)
            })
            wx.setStorageSync('userInfo', res.data)
        })
      },

    initOrderDetail() {
        if (!wx.getStorageSync('userInfo').userid) {
            toast('请先登陆')
        } 
        wx.p.request({
            method: 'POST',
            url: app.globalData.api + 'order/list',
            data: {
                userid: wx.getStorageSync('userInfo').userid
            }
        }).then(res => {
            this.setData({
                orderDetail: res.data
            })
            this.flush()
        })
    },

    goto(e) {
        wx.navigateTo({
          url: e.target.dataset.url,
        })
      },
    onLoad: function () {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: ['userInfo','userType'],
            actions: ['setUserInfo','setUserType']
        })
        this.initUserInfo()
        this.initOrderDetail()
    },
    flush() {
        var that = this
        setInterval(function () {
            var start = new Date(that.data.orderDetail[0].start)
            var end = new Date()
            // 计算时间差（毫秒）
            var timeDiff = end - start;
            // 将时间差转换为分钟
            var minutesDiff = timeDiff / 60000;
            that.setData({
                now: new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8),
                nowMoney:  Math.floor(minutesDiff) * 5
            })
        }, 1000)
    },
    onShow() {
        this.initUserInfo()
        this.initOrderDetail()
    },
    onUnload: function () {
        this.storeBindings.destroyStoreBindings()
    }
})